package com.briup.estore.web.servlet;

import com.briup.estore.bean.Book;
import com.briup.estore.bean.Category;
import com.briup.estore.bean.extend.CategoryExtend;
import com.briup.estore.service.BookService;
import com.briup.estore.service.CategoryService;
import com.briup.estore.service.impl.BookServiceImpl;
import com.briup.estore.service.impl.CategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 首页数据的获取
 */
@WebServlet("/index_data")
public class IndexDataServlet extends HttpServlet {

    private BookService bookService;
    private CategoryService categoryService;
    {
        bookService = new BookServiceImpl();
        categoryService = new CategoryServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1、获取service层提供的数据
        List<Book> books = bookService.findAllBooks();
        List<CategoryExtend> categoryList = categoryService.selectAll();
        // 2、保存在session里，名字自己取
        HttpSession session = req.getSession();
        session.setAttribute("books",books);
        session.setAttribute("categoryList",categoryList);
        // 3、跳转页面（测试的时候可以写响应）
        // resp.getWriter().println(books);
        // 重定向跳转
        resp.sendRedirect("index");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
